package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.baselibrary.view.MyGridView;
import com.cmccgz.baselibrary.view.adapter.BoxingAdapter;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.Media;
import com.cmccgz.udacs_landlord_webapp.bean.WarningDetail;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hzx07 on 2017/8/15.
 */

public class WarningDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final LayoutInflater inflater;
    private final Object link;
    private WarningDetail detail;
    private Context context;
    private List<WarningParam> datas = new ArrayList<>();
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
    private final static int TYPE_EMPTY = -1;
    private final static int TYPE_NORMAL = 1;
    private final static int TYPE_HAS_IMGE = 2;

    public WarningDetailAdapter( Context context, Object link) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.link = link;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_EMPTY:
                return new EmptyViewHolder(inflater.inflate(R.layout.item_empty, parent, false));
            case TYPE_NORMAL:
                return new NormalViewHolder(inflater.inflate(R.layout.item_warning_detail_normal, parent, false));
            case TYPE_HAS_IMGE:
                return new HasImgViewHolder(inflater.inflate(R.layout.item_warning_detail_normal_has_img, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        WarningParam warningParam;
        switch (getItemViewType(position)) {
            case -1:
                ((EmptyViewHolder) holder).tv_empty.setText("暂无详情，请下拉刷新！");
                break;
            case 1:
                warningParam = datas.get(position);
                final NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
                normalViewHolder.tv_w_label.setText(warningParam.label);
                normalViewHolder.tv_w_param.setText(warningParam.param);
                break;
            case TYPE_HAS_IMGE:
                warningParam = datas.get(position);
                final HasImgViewHolder hasImgViewHolder = (HasImgViewHolder) holder;
                hasImgViewHolder.tv_w_label.setText(warningParam.label);
                hasImgViewHolder.tv_w_param.setText(warningParam.param);
                hasImgViewHolder.gv.setVisibility(View.VISIBLE);
                final int numColumns = 4;
                hasImgViewHolder.gv.setNumColumns(numColumns);
                List<Media> extra = (List<Media>) warningParam.extra;
                final List<String> urls = new ArrayList<>();
                for (int i = 0; extra != null && i < extra.size(); i++) {
                    urls.add(extra.get(i).getImagePath());
                }
                ViewTreeObserver viewTreeObserver = hasImgViewHolder.tv_w_param.getViewTreeObserver();
                final boolean[] isMeasure = new boolean[]{false};
                viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {
                        if (isMeasure[0]) {
                            return true;
                        }
                        isMeasure[0] = true;
                        int measuredWidth = hasImgViewHolder.tv_w_param.getMeasuredWidth();
                        hasImgViewHolder.gv.setAdapter(new BoxingAdapter(false, hasImgViewHolder.gv, urls, measuredWidth, link, numColumns));
                        return true;
                    }
                });
                break;
        }
    }

    @Override
    public int getItemCount() {
        return detail == null || datas == null || datas.size() == 0 ? 1 : datas.size();
    }

    public void setWarningDetail(WarningDetail detail) {
        this.detail = detail;
        parseItem();
        notifyDataSetChanged();
    }

    ;

    @Override
    public int getItemViewType(int position) {
        if (detail == null || datas == null || datas.size() == 0) {
            return TYPE_EMPTY;
        }
        Object extra = datas.get(position).extra;
        return extra != null && extra instanceof List ? TYPE_HAS_IMGE : TYPE_NORMAL;
    }

    private void parseItem() {
        datas.clear();
        if (detail != null) {
            datas.add(new WarningParam("设备名称", detail.house_name));
            datas.add(new WarningParam("故障描述", detail.desc, detail.media));
            String w_state = "未知";
            switch (detail.w_state) {
                case "1":
                    w_state = "待派单";
                    break;
                case "2":
                    w_state = "待处理";
                    break;
                case "3":
                    w_state = "待确认";
                    break;
                case "4":
                    w_state = "已完成";
                    break;
            }
            datas.add(new WarningParam("状\u3000\u3000态", w_state));
            if (!StringUtils.isEmpty(detail.c_man)) {
                datas.add(new WarningParam("提  单  人", detail.c_man + (StringUtils.isEmpty(detail.c_phone) ? "" : "/" + detail.c_phone)));
            }
            if (!StringUtils.isEmpty(detail.c_timestamp)) {
                try {
                    datas.add(new WarningParam("提单时间", sdf.format(Long.parseLong(detail.c_timestamp))));
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
            if (!StringUtils.isEmpty(detail.d_man) && !"1".equals(detail.w_state)) {
                datas.add(new WarningParam("处  理  人", detail.d_man + (StringUtils.isEmpty(detail.d_phone) ? "" : "/" + detail.d_phone)));
            }
            if ("3".equals(detail.w_state) || "4".equals(detail.w_state)) {
                String d_type = "0".equals(detail.d_type) ? "设备维修" : "1".equals(detail.d_type) ? "替换设备" : "未知";
                datas.add(new WarningParam("处理方式", d_type));
                if (!StringUtils.isEmpty(detail.d_result)) {
                    datas.add(new WarningParam("处理结果", detail.d_result));
                }
                if (!StringUtils.isEmpty(detail.d_timestamp)) {
                    datas.add(new WarningParam("处理时间", sdf.format(Long.parseLong(detail.d_timestamp))));
                }
            }
        }
    }

    private class NormalViewHolder extends RecyclerView.ViewHolder {

        private final TextView tv_w_label;
        private final TextView tv_w_param;

        public NormalViewHolder(View itemView) {
            super(itemView);
            tv_w_label = (TextView) itemView.findViewById(R.id.tv_w_label);
            tv_w_param = ((TextView) itemView.findViewById(R.id.tv_w_param));
        }
    }

    private class HasImgViewHolder extends RecyclerView.ViewHolder {

        private final TextView tv_w_label;
        private final TextView tv_w_param;
        private final MyGridView gv;

        public HasImgViewHolder(View itemView) {
            super(itemView);
            tv_w_label = (TextView) itemView.findViewById(R.id.tv_w_label);
            tv_w_param = ((TextView) itemView.findViewById(R.id.tv_w_param));
            gv = (MyGridView) itemView.findViewById(R.id.gv);
        }
    }

    private class EmptyViewHolder extends RecyclerView.ViewHolder {
        private final TextView tv_empty;

        public EmptyViewHolder(View itemView) {
            super(itemView);
            tv_empty = ((TextView) itemView.findViewById(R.id.tv_empty));
        }

    }

    private class WarningParam {
        private String label;
        private String param;
        private Object extra;

        public WarningParam(String label, String param, Object extra) {
            this.label = label;
            this.param = param;
            this.extra = extra;
        }

        public WarningParam(String label, String param) {
            this.label = label;
            this.param = param;
        }
    }

    public WarningDetail getDetail() {
        return detail;
    }
}
